package cn.jobstart.service.impl;

import cn.jobstart.common.BeanUtils;
import cn.jobstart.common.SkMap;
import cn.jobstart.common.StringUtil;
import cn.jobstart.dao.IResumeEducationalExperienceDao;
import cn.jobstart.data.DataUtil;
import cn.jobstart.modelExt.ResumeEducationalExperienceExt;
import cn.jobstart.pubInter.AbstractBaseService;
import cn.jobstart.pubInter.IBaseDao;
import cn.jobstart.pubInter.exception.SkException;
import cn.jobstart.searchExt.ResumeEducationalExperienceSearchExt;
import cn.jobstart.service.IResumeEducationalExperienceService;
import cn.jobstart.vo.ResumeEducationalExperienceMiniDomainVO;
import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @author sunke
 * @Date 2023-09-06 15:00:21
 * @description ResumeEducationalExperienceService      教育经历
 */
@Service(value = "resumeEducationalExperienceService")
@Slf4j
public class ResumeEducationalExperienceServiceImpl extends AbstractBaseService<ResumeEducationalExperienceExt, ResumeEducationalExperienceSearchExt, String> implements IResumeEducationalExperienceService {

    @Resource(name = "resumeEducationalExperienceDao")
    private IResumeEducationalExperienceDao resumeEducationalExperienceDao;
    @Resource
    private DataUtil dataUtil;

    @Override
    public IBaseDao<ResumeEducationalExperienceExt, ResumeEducationalExperienceSearchExt, String> getDao() {
        return resumeEducationalExperienceDao;
    }

    @Override
    public ResumeEducationalExperienceExt insert(ResumeEducationalExperienceExt obj) throws SkException {
        obj.setSchoolLabelFlag(dataUtil.getSchoolLabelBySchoolName(obj.getTitle()));
        return super.insert(obj);
    }

    @Override
    public List<ResumeEducationalExperienceExt> batchInsert(List<ResumeEducationalExperienceExt> list) throws SkException {
        if (StringUtil.isNotObjEmpty(list)) {

            list.stream().forEach(item -> {

                item.setSchoolLabelFlag(dataUtil.getSchoolLabelBySchoolName(item.getTitle()));

            });


        }


        return super.batchInsert(list);
    }

    @Override
    public ResumeEducationalExperienceExt update(ResumeEducationalExperienceExt obj) throws SkException {
        obj.setSchoolLabelFlag(dataUtil.getSchoolLabelBySchoolName(obj.getTitle()));
        return super.update(obj);
    }

    /**
     * 教育经历
     *
     * @param resumeId
     * @return
     */
    @Override
    public List<ResumeEducationalExperienceExt> getListByResumeId(String resumeId) {
        if (StringUtil.isNullOrEmpty(resumeId)) {
            return null;
        }
        try {
            return getAllList(ResumeEducationalExperienceSearchExt.getInstance().setResumesIdEq(resumeId));
        } catch (SkException e) {
            return null;
        }
    }

    /**
     * @param resumeId
     * @return
     */
    @Override
    public Map<String, Object> getEducationalExperienceStrByResumeId(String resumeId) {
        try {
            List<ResumeEducationalExperienceExt> list = getListByResumeId(resumeId);
            if (StringUtil.isNullOrEmpty(list)) {
                return null;
            }
            ResumeEducationalExperienceExt lastEducation = null;
            if (list.size() == 1) {
                lastEducation = list.get(0);
            } else {

                list = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(user -> user.getTitle() + ";" + user.getUnitName()))), ArrayList::new));


                try {
                    lastEducation = list.stream().filter(item -> StringUtil.isNotObjEmpty(item.getEndTime())).sorted(Comparator.comparing(ResumeEducationalExperienceExt::getEndTime).reversed()).findFirst().orElse(null);
                } catch (Exception e) {


                }
                try {
                    if (StringUtil.isNullOrEmpty(lastEducation)) {

                        lastEducation = list.stream().sorted(Comparator.comparing(ResumeEducationalExperienceExt::getUpdateTime).reversed()).findFirst().orElse(null);
                    }
                } catch (Exception e) {

                }
            }

            if (StringUtil.isNullOrEmpty(lastEducation)) {
                lastEducation = list.get(list.size() - 1);
            }


            Map<String, Object> dataMap = SkMap.getInstance();

            dataMap.put("school", "");
            dataMap.put("speciality", "");
            dataMap.put("education", "");
            dataMap.put("info", "");
            dataMap.put("label", "");
            if (StringUtil.isNotObjEmpty(lastEducation)) {

                dataMap.put("school", lastEducation.getTitle());
                dataMap.put("speciality", lastEducation.getUnitName());
                dataMap.put("education", lastEducation.getUnitType());
                dataMap.put("label", lastEducation.getSchoolLabelFlag());

            }

            dataMap.put("info", JSONArray.toJSONString(list.stream().map(item -> BeanUtils.copyPropertiesIgnoreNull2(item, ResumeEducationalExperienceMiniDomainVO.class)).collect(Collectors.toList())));


/*
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter();

            filter.getIncludes().add("id");
            filter.getIncludes().add("title");
            filter.getIncludes().add("unitName");
            filter.getIncludes().add("startTime");
            filter.getIncludes().add("endTime");
            filter.getIncludes().add("unitType");
            filter.getIncludes().add("detail");

            dataMap.put("info",JSONArray.toJSONString(list, filter));*/


            return dataMap;


        } catch (Exception e) {

        }
        return null;

    }
}